package com.xilconic.achieveitall.fragments.goalsoverview;

import com.xilconic.achieveitall.R;
import com.xilconic.achieveitall.domain.Goal;
import com.xilconic.achieveitall.domain.GoalsOverview;
import com.xilconic.achieveitall.domain.Target;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class GoalsOverviewExpandableListAdapter extends
        BaseExpandableListAdapter {
    private GoalsOverview data;
    private Context context;

    public GoalsOverviewExpandableListAdapter(GoalsOverview data,
            Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return data.getActiveGoals().size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return getGoal(groupPosition).getTargets().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return getGoal(groupPosition);
    }

    private Goal getGoal(int groupPosition) {
        return data.getActiveGoals().get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return getGoal(groupPosition).getTargets().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        Goal goal = getGoal(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expandlist_parent_goal, null);
            
            TextView nameTextView = (TextView) convertView
                    .findViewById(R.id.expandlist_parent_goal_name_TextView);
            
            GroupViewHolder viewHolder = new GroupViewHolder();
            viewHolder.name = nameTextView;
            convertView.setTag(viewHolder);
        }
 
        GroupViewHolder viewHolder = (GroupViewHolder)convertView.getTag();
        viewHolder.name.setText(goal.getName());
        
        return convertView;
    }
    
    private class GroupViewHolder{
        public TextView name;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
    	Target target = getTarget(groupPosition, childPosition);
    	
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expandlist_child_target, null);
            
            CheckBox nameAndFinishedCheckBox = (CheckBox) convertView
                    .findViewById(R.id.expandlist_child_target_CheckBox);
            
            ChildViewHolder viewHolder = new ChildViewHolder();
            viewHolder.nameAndFinished = nameAndFinishedCheckBox;
            convertView.setTag(viewHolder);
        }
 
        ChildViewHolder viewHolder = (ChildViewHolder) convertView.getTag();
        viewHolder.nameAndFinished.setText(target.getName());
        viewHolder.nameAndFinished.setChecked(target.isFinished());
        
        return convertView;
    }

	private Target getTarget(int groupPosition, int childPosition) {
		Goal goal = getGoal(groupPosition);
    	Target target = goal.getTargets().get(childPosition);
		return target;
	}
    
    private class ChildViewHolder{
    	public CheckBox nameAndFinished;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
